/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.uhk.ppro.response.web;

import cz.uhk.ppro.response.form.StudentsCourses;
import cz.uhk.ppro.response.model.Test;
import cz.uhk.ppro.response.service.DatabaseService;
import cz.uhk.ppro.response.form.TestsCourses;
import cz.uhk.ppro.response.model.Course;
import cz.uhk.ppro.response.model.Student;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Insidel
 */

@Controller
@SessionAttributes({"TC"})
public class CoursesController {

    @Autowired
    private DatabaseService databaseSession;

    @RequestMapping(value="/smazKurz", method=RequestMethod.POST)
    protected String smaz(@RequestParam(value = "id") Integer id)
    {
        databaseSession.odstranKurz(id);

        return "redirect:/courses.do";
    }

    @RequestMapping("/courses.do")
    public ModelAndView zobrazit()
    {
        ModelAndView model = new ModelAndView("courses");

        Map<Integer, Integer> courses_test = new HashMap<>();
        List<Course> cs = databaseSession.getKurzy();
        for(Course c: cs) {
            courses_test.put(c.getId(), databaseSession.getTestByCourseCount(c));
        }
        model.addObject("testCount", courses_test);
        model.addObject("kurzy", cs);

        return model;
    }

     /*
     * Prolinkování KURZ - TEST
     */

    @ModelAttribute("testy")
    public List<Test> populateTests()
    {
        return databaseSession.getTestsList();
    }


    @RequestMapping(value = "/courses_tests.do", method = RequestMethod.GET)
    public ModelAndView zobrazitKurzyTesty(@RequestParam("id") int id)
    {
        ModelAndView model = new ModelAndView("formCoursesTests");
        TestsCourses tc = new TestsCourses();
        tc.setCourse_id(id);
        tc.setTests(databaseSession.getTestsIdsConnectedByCourses(databaseSession.getCourseById(id)));
        model.addObject("TC", tc);

        return model;
    }

    @RequestMapping(value = "/courses_tests.do", method = RequestMethod.POST)
    public String onSubmitCoursesTests(@ModelAttribute("CT") TestsCourses tc, @RequestParam("id") int id)
    {
        tc.setCourse_id(id);
        databaseSession.prolinkujKurzyTesty(tc);
        return "redirect:course.do?id="+id;
    }

    /*
     * Prolinkování KURZ - STUDENT
     */

    @ModelAttribute("studenti")
    public List<Student> populateStudents()
    {
        return databaseSession.getStudentsList();
    }


    @RequestMapping(value = "/courses_students.do", method = RequestMethod.GET)
    public ModelAndView zobrazitKurzyStudenti(@RequestParam("id") int id)
    {
        ModelAndView model = new ModelAndView("formCoursesStudents");
        StudentsCourses sc = new StudentsCourses();
        sc.setCourse_id(id);
        sc.setStudents(databaseSession.getStudentsIdsConnectedByCourses(databaseSession.getCourseById(id)));
        model.addObject("SC", sc);

        return model;
    }



    @RequestMapping(value = "/courses_students.do", method = RequestMethod.POST)
    public String onSubmitCoursesStudents(@ModelAttribute("CS") StudentsCourses sc, @RequestParam("id") int id)
    {
        sc.setCourse_id(id);
        databaseSession.prolinkujKurzyStudenty(sc);
        return "redirect:course.do?id="+id;
    }




}
